import 'package:casdon/common/widget/common_loading_widget.dart';
import 'package:flutter/material.dart';

// 一个简单的切换按钮组件
// 由父组件控制状态和操作回调
// 适用于关注/取消关注等场景
// TODO: 之后需要进一步提升抽象，然后放到common目录下
class ToggleButton extends StatelessWidget {
  final bool value; // 由父组件控制的状态
  final Future<bool> Function() onPressed; // 操作回调
  final double width;
  final double height;
  final bool isLoading; // 由父组件控制的加载状态

  const ToggleButton({
    super.key,
    required this.value,
    required this.onPressed,
    this.isLoading = false,
    this.width = 100,
    this.height = 36,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: isLoading ? null : _handleTap,
      child: Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
          color: value ? Colors.white : Colors.purple[50],
          borderRadius: BorderRadius.circular(height / 2),
          border: Border.all(
            color: value ? Colors.grey[300]! : Colors.purple,
            width: 1.5,
          ),
        ),
        alignment: Alignment.center,
        child: isLoading
            ? const SizedBox(
                width: 20,
                height: 20,
                child: CommonLoadingWidget(),
              )
            : Text(
                value ? '已关注' : '关注',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  color: value ? Colors.grey[700] : Colors.purple,
                ),
              ),
      ),
    );
  }

  Future<void> _handleTap() async {
    final success = await onPressed();
    // 注意：不再自行修改状态，完全由父组件控制
  }
}
